package org.mtmi.ui.actions;

import java.util.ArrayList;

import org.mtmi.ui.drawables.IDrawable;

/**
 * Basic implementation of action.<
 * <p>
 * Provide also IActionListener support to update action UI.
 *
 */
public class Action implements IAction
{
	private int type;
	private ArrayList<IActionListener> lis_action;
	
	
	/** create action using preferred type */
	public Action( int _type ) {
		type = _type;
	}
	
	@Override
	public int getType() {
		return type;
	}
	
	@Override /** @return null */
	public IDrawable getImage() {
		return null;
	}

	@Override /** @return null */
	public String getText() {
		return null;
	}

	@Override /** @return false */
	public boolean isEnabled() {
		return false;
	}
	
	@Override /** do nothing ... but do it very fine */
	public void run() {
	}

	public void addActionListener( IActionListener lis ) {
		if( lis_action ==null ) {
			lis_action = new ArrayList<IActionListener>();
		}
		lis_action.add( lis );
	}
	
	public void removeActionListener( IActionListener lis ) {
		if( lis_action !=null ) {
			lis_action.remove( lis );
		}
	}
	
	private void notifyActionChanged() {
		if( lis_action!=null ) {
			IActionListener lis[] = lis_action.toArray( new IActionListener[lis_action.size()] );
			for( IActionListener l : lis ) {
				l.actionChanged( this );
			}
		}
	}
	
	/** call all action listener to prevent them something as changed in action */
	public void update() {
		notifyActionChanged();
	}
}
